<?php
  if (!isset($_GET['Typo'])) {
  	$_GET['Typo'] = 3;
	}
  if (!isset($_GET['Group'])) { 
  	$Group = 'Office';} else {$Group = $_GET['Group'];
 	 }
  if (!isset($_GET['Group2'])) { 
  	$Group2 = 1;} else {$Group2 = $_GET['Group2'];
 	 }
  $TimeType = 'TimeClose';
  $OLDPAR = 0;
  $TOTAL = '0.00';
  $SUBTOTAL = '0.00';
  ?>
<style type="text/css">
<!--
.style2 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px;}
-->
</style>
<table width="98%"  border="1" cellspacing="0" cellpadding="4">
  <tr>
    <td colspan="3"><div align="center"></div>      
      <div align="center">Designação</div>      <div align="center"></div></td>
    <td colspan="2"><div align="center">Total</div></td>
  </tr>
  <?php /*if (isset($_GET['Office'])) {
  	$CCols = "SELECT main_offices.ref AS Colum
				FROM main_offices 
				WHERE main_offices.Type IN (1, 2)";
	$Cols = $GLOBALS['CONN']->db_array($CCols);  
	for ($t = 0; $t < $CTO; $t++) {*/
	
	if ($_GET['Offices'] != '') {
	 $offs = 'AND a.Office = '.$_GET['Offices'];
	 } else {
	 $offs = '';
	 }

	  $DEOFF = "SELECT 
					h.name AS Office,
					h.ref AS Ref,
					SUM(a.Amount) AS OfficeTot
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_GET['Time']}' AND '{$_GET['TimeEnd']}'
				AND b.Status = 2
				AND a.Status != 5
				AND b.Type != 8
				{$offs}
				GROUP BY h.ref
				ORDER BY h.ref ASC";
	  $EOFF = $GLOBALS['CONN']->db_array($DEOFF); 
	  for ($y = 1; $y <= $EOFF[0]; $y++) {
	    if ($EOFF[$y]['Ref'] == '') {$EOFF[$y]['Ref'] = 0; $EOFF[$y]['Office'] = 'N�o Atribuida';}
	  echo '<tr>
    <td colspan="5">
	 <table width="100%"  border="0" cellspacing="3" cellpadding="1">
        <tr>
          <td width="100%"><u>'.$EOFF[$y]['Office'].'</u></td>
          <td>'.$EOFF[$y]['OfficeTot'].'</td>
        </tr>
        <tr>
          <td colspan="2"><hr width="90%" color="000000" /></td>
        </tr>
      </table>
	</td>
  </tr>';
  

	  
	  $CResults = "SELECT 
					h.name AS Office,
					j.name AS User,
					g.name AS CODE,
					g.CODE AS COD,
					g.Type AS CODTYPE,
					g.ParentCODE AS PCOD,
					SUM(a.Amount) AS Exp,
					c.icon,
					k.name AS Provider
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				LEFT OUTER JOIN ccenter_contas AS g ON g.CODE IN (a.CODE, 

b.CODE)
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				LEFT OUTER JOIN main_users AS j ON j.ref = a.User
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref IN (a.Provider, 

b.RefProvider)
				INNER JOIN main_settings_currency AS c ON c.ref = b.RefCurrency
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_GET['Time']}' AND 

'{$_GET['TimeEnd']}'
				AND a.Office = {$EOFF[$y]['Ref']}
				AND b.Status = 2
				AND a.Status != 5
				AND b.Type != 8
				GROUP BY g.CODE
				ORDER BY g.CODE ASC";
	$Results = $GLOBALS['CONN']->db_array($CResults); 
	
	for ($f = 1; $f <= $Results[0]; $f++) {
			$TOTAL = $TOTAL+$Results[$f]['Exp'];
	if ($Results[$f]['PCOD'] != $OLDPAR) {
  ?>
  <tr>
    <td colspan="3"><strong><?php

      $DESG = "SELECT 
					h.name AS Office,
					j.name AS User,
					g.name,
					g.CODE AS COD,
					g.Type AS CODTYPE,
					g.ParentCODE AS PCOD,
					SUM(a.Amount) AS SUB,
					c.icon,
					k.name AS Provider
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON a.RefInvoice = b.Ref
				LEFT OUTER JOIN ccenter_contas AS g ON g.CODE IN (a.CODE, b.CODE)
				LEFT OUTER JOIN main_offices AS h ON h.ref = a.Office
				LEFT OUTER JOIN main_users AS j ON j.ref = a.User
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref IN (a.Provider, b.RefProvider)
				INNER JOIN main_settings_currency AS c ON c.ref = b.RefCurrency
				WHERE DATE(b.{$TimeType}) BETWEEN '{$_GET['Time']}' AND '{$_GET['TimeEnd']}'
				AND a.Office = {$EOFF[$y]['Ref']}
				AND g.CODE LIKE '{$Results[$f]['PCOD']}%'
				AND b.Status = 2
				AND b.Type != 8
				AND a.Status != 5
				GROUP BY g.ParentCODE";
	  $ESG = $GLOBALS['CONN']->db_array($DESG); 
	  
	  $DESGN = "SELECT name FROM ccenter_contas WHERE CODE LIKE '{$Results[$f]['PCOD']}%' AND CODE != 0";
	  $ESGN = $GLOBALS['CONN']->db_array($DESGN); 
	 if (isset($ESGN[1])) { echo $ESGN[1]['name'];}?></strong>
     </td>
    <td colspan="2" align="right"><strong><?php echo $ESG[1]['SUB'];?>
    </strong></td>
  </tr>
  <?php
  }
  $OLDPAR = $Results[$f]['PCOD'];
  if ($Results[$f]['CODTYPE'] == 1) {
  ?>
  <tr>
    <td 

colspan="3"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $Results[$f]['CODE'];?></strong></td>
    <td colspan="2" align="right"><strong><?php echo $Results[$f]['Exp'];?>
    </strong></td>
  </tr>
  <?php
  } elseif ($Results[$f]['CODTYPE'] == 0) { ?>
  <tr>
    <td align="right"><b><?php echo $Results[$f]['COD'];?></b>
      <div align="right"></div>
    <div align="right"></div></td>
    <td width="71%" colspan="2"><?php echo $Results[$f]['CODE'];?></td>
    <td width="15%" align="right"><?php echo $Results[$f]['Exp'];?></td>
    <td width="15%" align="right">&nbsp;</td>
  </tr>
  <?php	}
  	} 
}?>
    <tr>
    <td colspan="4">Total:</td>
    <td align="right"><b>� <?php echo $TOTAL;?></b></td>
  </tr>
</table>